package net.zjitc.controller.client;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Notice;
import net.zjitc.pojo.Products;
import net.zjitc.service.CategoryService;
import net.zjitc.service.NoticeService;
import net.zjitc.service.ProductService;
import net.zjitc.vo.Searchvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/15 8:30
 */
@Controller
public class IndexController {
    final String SALE_DAY_SEVEN = "7";
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;
    @Autowired
    NoticeService noticeService;
    public void top(Model model){
        List<Category> list = categoryService.findAll();
        model.addAttribute("list",list);
    }

    @RequestMapping(value = "/index")
    public String menuCategory(Model model){
        top(model);
        Notice notice = noticeService.details();
        model.addAttribute("n",notice);
        List<Products>productsList = productService.findHot(SALE_DAY_SEVEN);
        model.addAttribute("plist",productsList);
        return "/client/index.jsp";
    }


    @RequestMapping(value = "/SearchProduct")
    public String findByCondition(Searchvo vo,Model model){
        top(model);
        List<Products> list = productService.SearchProduct(vo);
        model.addAttribute("p",list);
        int size = list.size();
        model.addAttribute("size",size);
        return "/client/product_search_list.jsp";
    }

//    分页
    @RequestMapping(value = "/pageProducts")
    public String pageProducts(Integer currentPage,Model model,@RequestParam(required = false) Integer id){
        top(model);
        Category category = categoryService.findById(id);
        model.addAttribute("category",category);
        PageHelper.startPage(currentPage,6);
        List<Products> list = productService.finBycid(id);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "/client/product_Pagelist.jsp";
    }

//    详情页
    @RequestMapping(value = "/findProductById")
    public String findProductById(String id,Model model){
        top(model);
        Products products = productService.findById(id);
        model.addAttribute("p",products);
        return "/client/info.jsp";
    }



}
